package com.yaodian100.ec.campaign.batch;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;

import com.yaodian100.core.admin.entity.User;
import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.common.utils.StringUtils;
import com.yaodian100.core.mail.entity.Mail;
import com.yaodian100.core.mail.service.MailService;
import com.yaodian100.core.template.service.TemplateService;

public class CampaignBatch1111 {

	protected Logger logger = LoggerFactory.getLogger(getClass());
	private static SessionFactory sessionFactory;
	protected static ClassPathXmlApplicationContext ctx = null;

	private MailService mailService;
	private BaseManager baseManager;
	private TemplateService templateService;

	private static Calendar startDate = Calendar.getInstance();
	private static Calendar endDate = Calendar.getInstance();

	public CampaignBatch1111() {
		mailService = ctx.getBean(MailService.class);
		baseManager = ctx.getBean(BaseManager.class);
		templateService = ctx.getBean(TemplateService.class);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ctx = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml", "applicationContext-*.xml" });
		String username = System.getenv("USERNAME");
		if (StringUtils.isBlank(username)) {
			username = System.getenv("USER");
		}
		if (StringUtils.isBlank(username)) {
			username = "test00";
		}
		if (ctx != null && ctx.getBean("sessionFactory") != null) {
			sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
		}
		User tester = new User(username);
		SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(tester, "password"));

		if (args != null && args.length >= 1) {
			endDate.setTime(DateUtil.convertStringToDate("yyyyMMddHHmm", args[0]));
			startDate.setTime(endDate.getTime());
			startDate.add(Calendar.HOUR_OF_DAY, -1);
		} else {
			endDate.setTime(DateUtil.getCurrentTime());
			endDate.set(Calendar.MINUTE, endDate.getActualMinimum(Calendar.MINUTE));
			endDate.set(Calendar.SECOND, endDate.getActualMinimum(Calendar.SECOND));
			endDate.set(Calendar.MILLISECOND, endDate.getActualMinimum(Calendar.MILLISECOND));
			endDate.add(Calendar.HOUR_OF_DAY, -1);

			startDate.setTime(endDate.getTime());
			startDate.add(Calendar.HOUR_OF_DAY, -1);
		}

		CampaignBatch1111 job = new CampaignBatch1111();
		job.execute();
	}

	public void execute() {

		try {
			Map objs = new HashMap();
			Object[] orders = baseManager
					.queryByHql(
							"from com.yaodian100.ec.order.entity.Orders o where o.paymentSt not in ('30','60') and o.orderSt not in ('70','80','81','82','83') and o.domainSrc=? and o.paymentDt between ? and ? order by o.amount asc, o.orderDt asc",
							new Object[] { "www.yaodian100.com", startDate.getTime(), endDate.getTime() });
			// orderService.getList(0, -1, cri, new String[] { "amount asc", "orderDt asc" });
			objs.put("startDate", startDate.getTime());
			objs.put("endDate", endDate.getTime());
			// 1=0,2=0,3=1,4=1,5=2,6=2,7=3,8=3
			//
			objs.put("orders", new Object[] { orders[(orders.length / 2) - 2], orders[(orders.length / 2) - 1],
					orders[(orders.length / 2)] });
			Mail mail = templateService.formatToMail("campaign201111", objs);
			mail.addTo("William", "william.diao@yaodian100.com");
			mail.addTo("Sara", "sarah.yang@yaodian100.com");
			mail.addTo("Ice", "ice.ding@yaodian100.com");
			mail.addCc("Faye", "faye.chen@yaodian100.com");
			mail.addTo("Jeff", "jeff.ma@yaodian100.com");
			mailService.save(mail);

			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
